﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF6Sample
{
    class Image
    {   //参考一起帮的登录页面，绘制一个验证码图片，存放到当前项目中。验证码应包含：
        //随机字符串
        //混淆用的各色像素点
        //混淆用的直线（或曲线）

        public static void GetImage()
        {
            Random rnd = new Random();
            Bitmap image = new Bitmap(500, 300);
            Graphics g = Graphics.FromImage(image);    //在画板的基础上生成一个绘图对象
            g.Clear(Color.AliceBlue);           //添加底色
            for (int i = 0; i < 1; i++)
            {
                int a = rnd.Next(1, 500);
                int b = rnd.Next(1, 300);
                g.DrawLine(new Pen(Color.Black), new Point(0, 0), new Point(a, b)); //画直线
            }
            for (int i = 0; i < 1; i++)
            {
                int a = rnd.Next(100, 500);
                int b = rnd.Next(100, 300);
                g.DrawLine(new Pen(Color.White), new Point(0, 0), new Point(a, b)); //画直线
            }
            for (int i = 0; i < 1; i++)
            {
                int a = rnd.Next(200, 500);
                int b = rnd.Next(100, 300);
                int c = rnd.Next(300, 400);
                int d = rnd.Next(150, 200);
                g.DrawLine(new Pen(Color.Red), new Point(c, d), new Point(a, b));
            }
            for (int i = 0; i < 2; i++)
            {
                int a = rnd.Next(300, 500);
                int b = rnd.Next(150, 300);
                int c = rnd.Next(200, 300);
                int d = rnd.Next(100, 150);
                g.DrawLine(new Pen(Color.Green), new Point(c, d), new Point(a, b));
            }

            g.DrawString(GetString(),       //绘制字符串
                new Font("宋体", 150),                //指定字体
                new SolidBrush(Color.DarkRed),      //绘制时使用的刷子
                new PointF(20, 20));                  //左上角定位

            for (int i = 0; i < 10000; i++)
            {
                int a = rnd.Next(1, 500);
                int b = rnd.Next(1, 300);
                image.SetPixel(a, b, Color.BlueViolet);
                image.SetPixel(a, b, Color.OrangeRed);
                image.SetPixel(a, b, Color.Green);
            }
            //绘制一个像素的点

            image.Save(@"D:\C#语法\作业点评.jpg", ImageFormat.Jpeg);   //保存到文件

        }
        public static string GetString()
        {
            Random rnd = new Random();
            string rndparmeter = "AbCdE1234";
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 4; i++)
            {
                int a = rnd.Next(0, 9);
                sb.Append(rndparmeter[a]);

            }
            string parmeter = sb.ToString();
            return parmeter;
        }

    }
}
